package com.cuzofu.fs.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuzofu.fs.common.PageUtils;
import com.cuzofu.fs.common.Query;
import com.cuzofu.fs.common.exception.RRException;
import com.cuzofu.fs.dao.BlueprintDao;
import com.cuzofu.fs.entity.BlueprintEntity;
import com.cuzofu.fs.entity.BlueprintHistoryEntity;
import com.cuzofu.fs.entity.MajorEntity;
import com.cuzofu.fs.service.BlueprintHistoryService;
import com.cuzofu.fs.service.BlueprintService;
import com.cuzofu.fs.service.MajorService;
import com.cuzofu.fs.vo.BlueprintHistoryVO;
import com.cuzofu.fs.vo.BlueprintVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("blueprintService")
public class BlueprintServiceImpl extends ServiceImpl<BlueprintDao, BlueprintEntity> implements BlueprintService {

    @Autowired
    MajorService majorService;

    @Autowired
    BlueprintHistoryService blueprintHistoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BlueprintEntity> page = this.page(
                new Query<BlueprintEntity>().getPage(params),
                new QueryWrapper<BlueprintEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public BlueprintEntity create(BlueprintVO vo) {

        String mid = vo.getMid();
        if (StringUtils.isEmpty(mid)) {
            throw new RRException("缺少必要参数：mid");
        }
        MajorEntity major = majorService.getById(mid);
        if (major == null) {
            throw new RRException("数据不存在");
        }

        boolean isFirstTrial = true;
        String bid = vo.getId();
        if (StringUtils.isNotEmpty(bid)) {
            isFirstTrial = false;
        }

        BlueprintEntity entity = new BlueprintEntity();
        if (isFirstTrial) {
            // 初审上传
            entity.setPid(major.getPid());
            entity.setUid(major.getUid());
            entity.setMid(major.getId());
            entity.setName(vo.getName());
            entity.setUrl(vo.getUrl());
            entity.setVersion(0);
            this.save(entity);
        } else {
            // 复审上传
            entity = this.getById(bid);
            // 当前版本加1
            entity.setVersion(entity.getVersion() + 1);
            entity.setUrl(vo.getUrl());
            this.updateById(entity);
        }
        // 图纸历史版本记录（初审、复审）
        BlueprintHistoryEntity blueprintHistory = new BlueprintHistoryEntity();
        blueprintHistory.setBid(entity.getId());
        blueprintHistory.setMid(major.getId());
        blueprintHistory.setUid(major.getUid());
        blueprintHistory.setPid(major.getPid());
        blueprintHistory.setName(vo.getName());
        blueprintHistory.setUrl(vo.getUrl());
        blueprintHistory.setVersion(entity.getVersion());
        blueprintHistoryService.save(blueprintHistory);

        return entity;
    }

    @Override
    public List<BlueprintHistoryVO> queryBlueprintHistoryByMid(String mid) {
        return this.baseMapper.queryBpHistoryByMid(mid);
    }

    @Override
    public List<BlueprintVO> queryBpList(QueryWrapper<BlueprintVO> queryWrapper) {
        return this.baseMapper.queryBpList(queryWrapper);
    }

}